package com.czh.subject.application.controller;

import com.czh.subject.application.convert.SubjectInfoConverter;
import com.czh.subject.application.dto.SubjectInfoDTO;
import com.czh.subject.common.entity.PageResult;
import com.czh.subject.common.entity.ResponseResult;
import com.czh.subject.domain.entity.SubjectInfoBO;
import com.czh.subject.domain.service.SubjectInfoDomainService;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author czh01
 * @version 1.0
 * @description: TODO
 * @date 2024/7/31 上午 12:50
 */
@RestController
@RequestMapping("/subject")
@Log4j2
public class SubjectInfoController {

    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;


    /**
     * 新增题目
     */
    @PostMapping("/add")
    public ResponseResult<Boolean> addSubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectName(),"题目名不能为空!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(),"题目难度不能为空!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(),"题目类型不能为空!");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(),"题目分值不能为空!");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryIds(),"题目分类不能为空!");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelIds(),"题目标签不能为空!");

            SubjectInfoBO subjectInfoBO = SubjectInfoConverter.INSTANCE.convert(subjectInfoDTO);
            return ResponseResult.success(subjectInfoDomainService.addSubject(subjectInfoBO));
        } catch (Exception e) {
            log.error("SubjectInfoController.add.error:{}", e.getMessage(), e);
            return ResponseResult.error("新增题目失败！");
        }
    }

    /**
     * 题目列表
     */
    @PostMapping("/getSubjectPage")
    public ResponseResult<PageResult<SubjectInfoBO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(),"题目标签不能为空!");
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(),"题目分类不能为空!");

            SubjectInfoBO subjectInfoBO = SubjectInfoConverter.INSTANCE.convert(subjectInfoDTO);
            PageResult<SubjectInfoBO> subjectPageBO = subjectInfoDomainService.getSubjectPage(subjectInfoBO);
//            PageResult<SubjectInfoDTO> subjectPageDTO = SubjectInfoConverter.INSTANCE.convert(subjectPageBO);
            return ResponseResult.success(subjectPageBO);
        } catch (Exception e) {
            log.error("SubjectInfoController.getSubjectPage.error:{}", e.getMessage(), e);
            return ResponseResult.error("获取题目列表失败！");
        }
    }

    /**
     * 题目详情
     */
    @PostMapping("/querySubjectInfo")
    public ResponseResult<SubjectInfoDTO> querySubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            Preconditions.checkNotNull(subjectInfoDTO.getId(),"题目id不能为空!");
            SubjectInfoBO subjectInfoBO = SubjectInfoConverter.INSTANCE.convert(subjectInfoDTO);
            SubjectInfoBO subjectInfo = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO resultDto = SubjectInfoConverter.INSTANCE.convert(subjectInfo);
            return ResponseResult.success(resultDto);
        } catch (Exception e) {
            log.error("SubjectInfoController.querySubjectInfo.error:{}", e.getMessage(), e);
            return ResponseResult.error("获取题目详情失败！");
        }
    }

    /**
     * 检索
     */
    @PostMapping("/getSubjectPageBySearch")
    public ResponseResult<PageResult<SubjectInfoDTO>> getSubjectPageBySearch(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            Preconditions.checkNotNull(subjectInfoDTO.getKeyWord(),"关键词不能为空!");
            SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
            subjectInfoBO.setKeyWord(subjectInfoDTO.getKeyWord());
            PageResult<SubjectInfoBO> subjectInfoBOPageResult = subjectInfoDomainService.searchSubjectInfo(subjectInfoBO);
            List<SubjectInfoDTO> subjectInfoDTOS = SubjectInfoConverter.INSTANCE.convert(subjectInfoBOPageResult.getResult());
            PageResult<SubjectInfoDTO> result = new PageResult<>();
            result.setPageNo(subjectInfoBOPageResult.getPageNo());
            result.setPageSize(subjectInfoBOPageResult.getPageSize());
            result.setResult(subjectInfoDTOS);

            return ResponseResult.success(result);
        } catch (Exception e) {
            log.error("SubjectInfoController.querySubjectInfo.error:{}", e.getMessage(), e);
            return ResponseResult.error("搜索失败！");
        }
    }

    /**
     * 贡献榜
     */
    @PostMapping("/getContributeList")
    public ResponseResult<List<SubjectInfoDTO>> getContributeList() {
        try {
            List<SubjectInfoBO> contributeBOList = subjectInfoDomainService.getContributeList();
            List<SubjectInfoDTO>  contributeDTOList = SubjectInfoConverter.INSTANCE.convert(contributeBOList);
            return ResponseResult.success(contributeDTOList);
        }catch (Exception e){
            log.error("SubjectInfoController.getContributeList.error:{}", e.getMessage(), e);
            return ResponseResult.error("获取排行榜失败！");
        }
    }
}
